package manage.commons.interceptors;

import javax.servlet.http.HttpServletRequest;

import manage.commons.Constants;
import manage.commons.annontations.SuperManager;
import manage.commons.cookie.UserStateSessionManager;
import manage.commons.utils.AnnontationUtils;
import manage.commons.utils.CookieUtils;

import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 
 * Description：超级管理员拦截器
 * 
 * @since 2012-9-16 下午9:16:17
 * @author dongji
 * 
 */
public class SuperManagerInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = -5994645434716200748L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		final ActionContext context = invocation.getInvocationContext();
		final ActionProxy proxy = invocation.getProxy();
		final Object action = invocation.getAction();
		final HttpServletRequest request = (HttpServletRequest) context
				.get(StrutsStatics.HTTP_REQUEST);
		final SuperManager require = AnnontationUtils.getFromMethedOrType(
				SuperManager.class, action, proxy.getMethod());
		if (require != null) {
			String cookieUser = CookieUtils.getCookie(request,
					Constants.COOKIE_U);
			final UserStateSessionManager sessionManager = new UserStateSessionManager(
					cookieUser);
			if (null == sessionManager || !sessionManager.isSuperManager()) {
				return Constants.UN_AUTHOR;
			}
		}
		return invocation.invoke();
	}

}
